package com.sais.manage.boot.cms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.utils.UUIDUtils;
import com.sais.manage.boot.cms.dto.ArticleDTO;
import com.sais.manage.boot.cms.dto.ArticleDetailsDTO;
import com.sais.manage.boot.cms.dto.ArticleSaveDTO;
import com.sais.manage.boot.cms.dto.ArticleSearchDTO;
import com.sais.manage.boot.cms.entity.Article;
import com.sais.manage.boot.cms.entity.Content;
import com.sais.manage.boot.cms.mapper.ArticleMapper;
import com.sais.manage.boot.cms.service.IArticleService;
import com.sais.manage.boot.cms.service.IContentService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 博客-文章 service 实现类
 *
 * @author zwp
 * @date 2020/02/16
 **/
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IContentService contentService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveArticle(ArticleSaveDTO articleSaveDTO) {
        String ukId = articleSaveDTO.getUkId();
        if (StringUtils.isEmpty(ukId)) {
            createArticle(articleSaveDTO);
        } else {
            updateArticle(articleSaveDTO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean createArticle(ArticleSaveDTO articleSaveDTO) {
        if(StringUtils.isEmpty(articleSaveDTO.getStatus())){
            articleSaveDTO.setStatus(0);
        }
        if(StringUtils.isEmpty(articleSaveDTO.getType())){
            articleSaveDTO.setType(0);
        }
        Article article = articleSaveDTO.convert(articleSaveDTO);

        article.setUkId(UUIDUtils.getLower());

        String ukId = ShiroUtils.getLoginUser().getUkId();
        article.setUserId(ukId);

        String articleContent = articleSaveDTO.getContent();
        Content content = Content.builder()
                .articleId(article.getUkId())
                .content(articleContent)
                .build();
        contentService.createContent(content);

        articleMapper.createArticle(article);
        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateArticle(ArticleSaveDTO articleSaveDTO) {
        Article article = articleSaveDTO.convert(articleSaveDTO);
        article.setGmtModified(LocalDateTime.now());
        articleMapper.updateById(article);

        String content = articleSaveDTO.getContent();
        contentService.updateContent(article.getUkId(), content);

        return true;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteArticle(String articleId) {
        articleMapper.deleteArticle(articleId);
        contentService.deleteContentByArticleId(articleId);
        return true;
    }

    @Override
    public ArticleDTO selectArticle(String articleId) {
        ArticleDTO article = articleMapper.selectArticle(articleId);
        return article;
    }

    @Override
    public Page<ArticleDTO> listArticle(ArticleSearchDTO searchDTO, Integer current, Integer size) {
        Page<ArticleDTO> listArticle = articleMapper.listArticle(new Page<>(current, size), searchDTO);
        return listArticle;
    }

    @Override
    public ArticleDetailsDTO selectArticleDetails(String articleId) {
        ArticleDetailsDTO articleDetailsDTO = articleMapper.selectArticleDetails(articleId);
        return articleDetailsDTO;
    }

    @Override
    public List<ArticleDTO> listArticleByCurrentUserId(String userId) {
        List<ArticleDTO> articles = articleMapper.listArticleByCurrentUserId(userId);
        return articles;
    }

    @Override
    public boolean updateArticleStatus(String articleId) {
        articleMapper.updateArticleStatus(articleId);
        return true;
    }
}
